from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.jkyx_service import JkyxService
from module_admin.entity.vo.jkyx_vo import DeleteJkyxModel, JkyxModel, JkyxPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


jkyxController = APIRouter(prefix='/function/jkyx', dependencies=[Depends(LoginService.get_current_user)])


@jkyxController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('function:jkyx:list'))]
)
async def get_function_jkyx_list(
    request: Request,
jkyx_page_query: JkyxPageQueryModel = Depends(JkyxPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    jkyx_page_query_result = await JkyxService.get_jkyx_list_services(query_db, jkyx_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=jkyx_page_query_result)


@jkyxController.post('', dependencies=[Depends(CheckUserInterfaceAuth('function:jkyx:add'))])
@ValidateFields(validate_model='add_jkyx')
@Log(title='监控影像处理记录', business_type=BusinessType.INSERT)
async def add_function_jkyx(
    request: Request,
    add_jkyx: JkyxModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_jkyx.user_id = current_user.user.user_id
    add_jkyx.user_name = current_user.user.user_name
    add_jkyx.create_time = datetime.now()
    add_jkyx_result = await JkyxService.add_jkyx_services(query_db, add_jkyx)
    logger.info(add_jkyx_result.message)

    return ResponseUtil.success(msg=add_jkyx_result.message)


@jkyxController.put('', dependencies=[Depends(CheckUserInterfaceAuth('function:jkyx:edit'))])
@ValidateFields(validate_model='edit_jkyx')
@Log(title='监控影像处理记录', business_type=BusinessType.UPDATE)
async def edit_function_jkyx(
    request: Request,
    edit_jkyx: JkyxModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_jkyx.update_by = current_user.user.user_name
    edit_jkyx.update_time = datetime.now()
    edit_jkyx_result = await JkyxService.edit_jkyx_services(query_db, edit_jkyx)
    logger.info(edit_jkyx_result.message)

    return ResponseUtil.success(msg=edit_jkyx_result.message)


@jkyxController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('function:jkyx:remove'))])
@Log(title='监控影像处理记录', business_type=BusinessType.DELETE)
async def delete_function_jkyx(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_jkyx = DeleteJkyxModel(ids=ids)
    delete_jkyx_result = await JkyxService.delete_jkyx_services(query_db, delete_jkyx)
    logger.info(delete_jkyx_result.message)

    return ResponseUtil.success(msg=delete_jkyx_result.message)


@jkyxController.get(
    '/{id}', response_model=JkyxModel, dependencies=[Depends(CheckUserInterfaceAuth('function:jkyx:query'))]
)
async def query_detail_function_jkyx(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    jkyx_detail_result = await JkyxService.jkyx_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=jkyx_detail_result)


@jkyxController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('function:jkyx:export'))])
@Log(title='监控影像处理记录', business_type=BusinessType.EXPORT)
async def export_function_jkyx_list(
    request: Request,
    jkyx_page_query: JkyxPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    jkyx_query_result = await JkyxService.get_jkyx_list_services(query_db, jkyx_page_query, is_page=False)
    jkyx_export_result = await JkyxService.export_jkyx_list_services(request, jkyx_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(jkyx_export_result))
